package com.superman.supermarket.client.composite.addrlist;

import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.supermarket.client.composite.IUIModule;
import com.superman.supermarket.client.event.AddrListPageEvent;
import com.superman.supermarket.client.event.AddrListPageEvent.AddrListPageEventType;

/**
 * 地址列表元素模块
 * 
 * @author superman
 * @version 2018年8月2日下午2:12:53
 */
public class AddrListCellModule extends Composite implements IUIModule {

	interface AddrListCellModuleUiBinder extends UiBinder<Widget, AddrListCellModule> {
	}

	private static AddrListCellModuleUiBinder uiBinder = GWT.create(AddrListCellModuleUiBinder.class);

	private boolean isDefault;
	@UiField
	protected FlowPanel mainpanel, setdefaultpanel, deletepanel;

	protected Map<String, Object> map;
	@UiField
	protected HTML receivernamehtml, telhtml, addrhtml, defaulticon, setdefaulthtml, deleteicon, deletehtml;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月2日 下午10:45:54
	 */
	public AddrListCellModule() {
		initWidget(uiBinder.createAndBindUi(this));

		mainpanel.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				event.stopPropagation();
				XGinjector.INSTANCE.getEventbusController().fire(new AddrListPageEvent(map, AddrListPageEventType.CLICK_TO_EDIT));

			}
		}, ClickEvent.getType());

		setdefaultpanel.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				event.stopPropagation();
				if (!isDefault)
					XGinjector.INSTANCE.getEventbusController()
							.fire(new AddrListPageEvent(String.valueOf(map.get("ADDR_ID")), AddrListPageEventType.SET_DEFAULT));

			}
		}, ClickEvent.getType());

		deletepanel.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				event.stopPropagation();
				XGinjector.INSTANCE.getEventbusController()
						.fire(new AddrListPageEvent(String.valueOf(map.get("ADDR_ID")), AddrListPageEventType.DELETE));
			}
		}, ClickEvent.getType());
	}

	@Override
	public Map<String, Object> getData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setData(Map<String, Object> map) {
		this.map = map;

		receivernamehtml.setText(String.valueOf(map.get("RECEIVER")));
		telhtml.setText(String.valueOf(map.get("PHONE_NO")));
		addrhtml.setText(String.valueOf(map.get("RECEIVE_AREA")) + String.valueOf(map.get("RECEIVE_ADDR")));

		isDefault = Double.parseDouble(String.valueOf(map.get("IS_DEFAULT"))) == 1;

		if (isDefault)
			defaulticon.addStyleName("fa-check-circle");
		else
			defaulticon.addStyleName("fa-circle-thin");

	}

}
